package com.supplychainapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.enums.ExceptionMessage;
import com.supplychainapi.dao.prod.TbdepartmentMapper;
import com.supplychainapi.domain.DepartmentDto;
import com.supplychainapi.model.Tbdepartment;
import com.supplychainapi.param.ColTitle;
import com.supplychainapi.service.ITbdepartmentService;
import com.supplychainapi.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class ITbdepartmentServiceImpl implements ITbdepartmentService {

    @Autowired
    TbdepartmentMapper tbdepartmentMapper;

    @Override
    public boolean save(Tbdepartment model)throws ServiceException {
        model.setFsDepartmentId(StringUtil.autoGenericCode(tbdepartmentMapper.getCode(),tbdepartmentMapper.getCode().length()));
        int b=tbdepartmentMapper.nameIsExit(model.getFsDepartmentName(),model.getFsShopGUID());
        model.setFsUpdateTime(new Date());
        if(b>0){
            throw ExceptionMessage.material_name_is_exit.getServiceException();
        }
        int a=tbdepartmentMapper.insertSelective(model);
        return a > 0;
    }

    @Override
    public boolean update(Tbdepartment model)throws ServiceException {
        int b=tbdepartmentMapper.nameIsExit(model.getFsDepartmentName(),model.getFsShopGUID());
        if(b>1){
            throw ExceptionMessage.material_name_is_exit.getServiceException();
        }
        int a=tbdepartmentMapper.updateByPrimaryKeySelective(model);
        return a > 0;
    }

    @Override
    public boolean delete(Tbdepartment model) throws ServiceException{
        int b=tbdepartmentMapper.selectQuotesById(model.getFsDepartmentId(),model.getFsShopGUID());
        if(b>0){
            throw ExceptionMessage.delete_treebuild_failed.getServiceException();
        }
        int a=tbdepartmentMapper.deleteByPrimaryKey(model);
        return a > 0;

    }

    @Override
    public Tbdepartment selectById(Tbdepartment model) throws ServiceException{
        return tbdepartmentMapper.selectByPrimaryKey(model);

    }

    @Override
    public PageInfo<DepartmentDto> selectAll(Tbdepartment model,Integer pageNum,Integer pageSize)throws ServiceException {
        PageHelper.startPage(pageNum,pageSize);
        List<Tbdepartment> list=tbdepartmentMapper.select(model);
        List<DepartmentDto> reulst= Lists.newArrayList();
        for (Tbdepartment tbdepartment : list) {
            DepartmentDto dto=new DepartmentDto();
            BeanUtils.copyProperties(tbdepartment,dto);
            if(dto.getFiStatus()==1){
                dto.setFiStatusName("正常");
            }
            if(dto.getFiStatus()==9){
                dto.setFiStatusName("禁用");
            }
            reulst.add(dto);
        }
        return new PageInfo(reulst);
    }

    @Override
    public PageInfo<DepartmentDto> search(Tbdepartment model,Integer pageNum,Integer pageSize) throws ServiceException {
        PageHelper.startPage(pageNum,pageSize);
        List<Tbdepartment> list = tbdepartmentMapper.search(model.getFsShopGUID(), model.getFsDepartmentName(), model.getFiStatus());
        List<DepartmentDto> reulst= Lists.newArrayList();
        for (Tbdepartment tbdepartment : list) {
            DepartmentDto dto=new DepartmentDto();
            BeanUtils.copyProperties(tbdepartment,dto);
            if(dto.getFiStatus()==1){
                dto.setFiStatusName("正常");
            }
            if(dto.getFiStatus()==9){
                dto.setFiStatusName("禁用");
            }
            reulst.add(dto);
        }
        return new PageInfo(reulst);

    }


    @Override
    public List<ColTitle> getSchema() {
        List<ColTitle> colTitles= Lists.newArrayList();
        colTitles.add(new ColTitle("fsDepartmentName","部门名称","center",100));
        colTitles.add(new ColTitle("fiStatus","状态","center",100));
        colTitles.add(new ColTitle("__EDIT__", "操作", null, 150, "fsDepartmentId"));
        return colTitles;
    }

    @Override
    public List<ColTitle> getSelectSchema() {
        List<ColTitle> colTitles= Lists.newArrayList();
        colTitles.add(new ColTitle("fsDepartmentName","部门名称","center",100));
        return colTitles;
    }

    @Override
    public List<DepartmentDto> getAll(String shopGuid) {
        Tbdepartment model = new Tbdepartment();
        model.setFsShopGUID(shopGuid);
        model.setFiStatus((short) 1);
        List<Tbdepartment> list = tbdepartmentMapper.select(model);
        List<DepartmentDto> reulst = Lists.newArrayList();
        for (Tbdepartment tbdepartment : list) {
            DepartmentDto dto = new DepartmentDto();
            BeanUtils.copyProperties(tbdepartment, dto);
            reulst.add(dto);
        }
        return reulst;
    }
}
